package com.baobao.web.rest;

import com.baobao.dao.ChatDAO;

import com.baobao.domain.Chat;

import com.baobao.service.ChatService;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for Chat entities
 * 
 */

@Controller("ChatRestController")
public class ChatRestController {

	/**
	 * DAO injected by Spring that manages Chat entities
	 * 
	 */
	@Autowired
	private ChatDAO chatDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Chat entities
	 * 
	 */
	@Autowired
	private ChatService chatService;

	/**
	 * Show all Chat entities
	 * 
	 */
	@RequestMapping(value = "/Chat", method = RequestMethod.GET)
	@ResponseBody
	public List<Chat> listChats() {
		return new java.util.ArrayList<Chat>(chatService.loadChats());
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Delete an existing Chat entity
	 * 
	 */
	@RequestMapping(value = "/Chat/{chat_sender}/{chat_receiver}/{chat_date}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteChat(@PathVariable String chat_sender, @PathVariable String chat_receiver, @PathVariable Calendar chat_date) {
		Chat chat = chatDAO.findChatByPrimaryKey(chat_sender, chat_receiver, chat_date);
		chatService.deleteChat(chat);
	}

	/**
	 * Save an existing Chat entity
	 * 
	 */
	@RequestMapping(value = "/Chat", method = RequestMethod.PUT)
	@ResponseBody
	public Chat saveChat(@RequestBody Chat chat) {
		chatService.saveChat(chat);
		return chatDAO.findChatByPrimaryKey(chat.getSender(), chat.getReceiver(), chat.getDate());
	}

	/**
	 * Select an existing Chat entity
	 * 
	 */
	@RequestMapping(value = "/Chat/{chat_sender}/{chat_receiver}/{chat_date}", method = RequestMethod.GET)
	@ResponseBody
	public Chat loadChat(@PathVariable String chat_sender, @PathVariable String chat_receiver, @PathVariable Calendar chat_date) {
		return chatDAO.findChatByPrimaryKey(chat_sender, chat_receiver, chat_date);
	}

	/**
	 * Create a new Chat entity
	 * 
	 */
	@RequestMapping(value = "/Chat", method = RequestMethod.POST)
	@ResponseBody
	public Chat newChat(@RequestBody Chat chat) {
		chatService.saveChat(chat);
		return chatDAO.findChatByPrimaryKey(chat.getSender(), chat.getReceiver(), chat.getDate());
	}
}